package com.smartClassroom.content.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartClassroom.base.exception.SmartClassroomException;
import com.smartClassroom.content.mapper.TeachplanMapper;
import com.smartClassroom.content.mapper.TeachplanMediaMapper;
import com.smartClassroom.content.model.dto.BindTeachplanMediaDto;
import com.smartClassroom.content.model.dto.SaveTeachplanDto;
import com.smartClassroom.content.model.dto.TeachplanDto;
import com.smartClassroom.content.model.po.Teachplan;
import com.smartClassroom.content.model.po.TeachplanMedia;
import com.smartClassroom.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    private TeachplanMapper teachplanMapper;
    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> getTreeNodes(Long courseId) {
        List<TeachplanDto> teachplanDto =  teachplanMapper.getTreeNodes(courseId);
        return teachplanDto;
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto saveTeachplanDto) {
        Long id = saveTeachplanDto.getId();
        if (id!=null){
            //修改操作
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }else {
            //新增操作
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachplanDto,teachplan);
            //获取排序数
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getCourseId,saveTeachplanDto.getCourseId())
                    .eq(Teachplan::getParentid,saveTeachplanDto.getParentid());
            Integer count = teachplanMapper.selectCount(queryWrapper);

            teachplan.setOrderby(count+1);
            teachplanMapper.insert(teachplan);
        }
    }

    @Transactional
    @Override
    public void deleteTeachplan(Long id) {
        if (id==null){
            SmartClassroomException.cast("请输入需要删除的课程计划id");
        }
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid,id);
        Integer count = teachplanMapper.selectCount(queryWrapper);//存在子节点不可删除
        if (count>0){
            SmartClassroomException.err("120409","课程计划信息还有子级信息，无法操作");
        }

        teachplanMapper.deleteById(id);

        LambdaQueryWrapper<TeachplanMedia> mediaQueryWrapper = new LambdaQueryWrapper<>();
        mediaQueryWrapper.eq(TeachplanMedia::getTeachplanId,id);
        teachplanMediaMapper.delete(mediaQueryWrapper);
    }

    @Override
    @Transactional
    public void movedown(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getOrderby,teachplan.getOrderby()+1)
                .eq(Teachplan::getGrade,teachplan.getGrade())
                .eq(Teachplan::getCourseId,teachplan.getCourseId())
                .eq(Teachplan::getParentid,teachplan.getParentid());
        Teachplan selectOne = teachplanMapper.selectOne(queryWrapper);
        if (selectOne!=null){
            Integer orderby = selectOne.getOrderby();
            selectOne.setOrderby(teachplan.getOrderby());
            teachplan.setOrderby(orderby);
            teachplanMapper.updateById(selectOne);
            teachplanMapper.updateById(teachplan);
        }
    }

    @Override
    public void moveup(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getOrderby,teachplan.getOrderby()-1)
                .eq(Teachplan::getGrade,teachplan.getGrade())
                .eq(Teachplan::getCourseId,teachplan.getCourseId())
                .eq(Teachplan::getParentid,teachplan.getParentid());
        Teachplan selectOne = teachplanMapper.selectOne(queryWrapper);
        if (selectOne!=null){
            Integer orderby = selectOne.getOrderby();
            selectOne.setOrderby(teachplan.getOrderby());
            teachplan.setOrderby(orderby);
            teachplanMapper.updateById(selectOne);
            teachplanMapper.updateById(teachplan);
        }
    }

    @Transactional
    @Override
    public void associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        Teachplan teachplan = teachplanMapper.selectById(bindTeachplanMediaDto.getTeachplanId());
        if(teachplan==null){
            SmartClassroomException.cast("教学计划不存在");
        }
        if (teachplan.getGrade()!=2){
            SmartClassroomException.cast("只允许第二级教学计划绑定媒资文件");
        }

        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId,bindTeachplanMediaDto.getTeachplanId());
        teachplanMediaMapper.delete(queryWrapper);


        TeachplanMedia teachplanMedia = new TeachplanMedia();
        BeanUtils.copyProperties(bindTeachplanMediaDto,teachplanMedia);
        teachplanMedia.setCourseId(teachplan.getCourseId());
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
    }

    @Override
    public void deleteAssociationMedia(Long teachplanId, String mediaId) {
        LambdaQueryWrapper<TeachplanMedia> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId,teachplanId).eq(TeachplanMedia::getMediaId,mediaId);
        teachplanMediaMapper.delete(queryWrapper);
    }
}
